/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.controller.marker;

import foxmob.mlts.component.FileManager;
import foxmob.mlts.component.ScoreManager;
import foxmob.mlts.interfaces.marker.MarkAnswerDetailPanelDelegate;
import foxmob.mlts.model.MultipleFillInBlankQuestion;
import foxmob.mlts.model.Question;
import foxmob.mlts.model.QuestionPaper;
import foxmob.mlts.model.Section;
import foxmob.mlts.view.marker.MarkAnswerDetailPanel;
import foxmob.mlts.viewcontroller.marker.MarkAnswerForm;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Lunayo
 */
public class MarkAnswerFormController implements MarkAnswerDetailPanelDelegate {
    
    private QuestionPaper testRecord;
    private boolean[] assessmentBlank;
    private MarkAnswerDetailPanel markAnswerPanel;
    
    private MarkAnswerForm delegate;
    
    public MarkAnswerFormController(MarkAnswerForm delegate) {
        this.delegate = delegate;
        initDetailComponents();
    }
    
    /**
     * notification delegate
     */
    @Override
    public void setAssessmentBlankPanelDidFinish() {
    }
    
    @Override
    public void assessmentWrongRadioButtonDidTap(int number, Question question) {
        assessmentBlank[number - 1] = false;
    }

    @Override
    public void assessmentCorrectRadioButtonDidTap(int number, Question question) {
        assessmentBlank[number - 1] = true;
    }
    
    @Override
    public void saveButtonDidTap(ActionEvent evt, Question question) {
        MultipleFillInBlankQuestion multipleFillinQuestion =
                    (MultipleFillInBlankQuestion) question;
            multipleFillinQuestion.setBlankAssesment(assessmentBlank);
        try {
            ScoreManager scoreManager = ScoreManager.getInstanceObject();
            double score = scoreManager.getQuestionPaperScore(testRecord);
            delegate.getCurrentScoreLabel().setText(scoreManager.convertToPercentage(score));
            // save to file
            FileManager manager = FileManager.getInstanceObject();
            manager.saveToFile(testRecord);
            String message = "Marking saved successfully!";
            JOptionPane.showMessageDialog(null, message);
        } catch (IOException ex) {
            Logger.getLogger(MarkAnswerForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
//        loadTestRecord();
        
    }
    
    /**
     * Creates new form MarkAnswerForm
     */
    
    private void initDetailComponents() {
        this.markAnswerPanel = new MarkAnswerDetailPanel();
        markAnswerPanel.setDelegate(this);
    }
    
    private void loadTestRecord() {
        FileManager manager = FileManager.getInstanceObject();
        this.testRecord = 
                manager.getTestRecord(FileManager.STUDENT_ANSWER_FILE);

    }
    
    private ArrayList<Question> loadQuestion() {
        loadTestRecord();
        ArrayList<Question> listOfQuestions =
                new ArrayList<Question>();
        
        for (Section section : this.testRecord.getSections()) {
            ArrayList<Question> multipleFillInQuestion =
                section.getQuestions(Question.QuestionType.MULTIPLE_FILL_IN_THE_BLANK);
            for (Question question : multipleFillInQuestion) {
                MultipleFillInBlankQuestion multipleFillIn =
                        (MultipleFillInBlankQuestion) question;
                if (multipleFillIn.isCorrect()) continue;
                listOfQuestions.add(question);
            }
        }
        return listOfQuestions;
    } 
    
    public void loadFillInListData() {
        delegate.getFillInBlankList().setListData(loadQuestion().toArray());
    }
    
    private void invokeComponent(Question question) {
        MultipleFillInBlankQuestion multipleFillinQuestion =
                (MultipleFillInBlankQuestion) question;
        MarkAnswerDetailPanel panel = 
                (MarkAnswerDetailPanel)delegate.getDetailPanel().getComponent(0);
        assessmentBlank = new boolean[multipleFillinQuestion.getBlankCount()]; 
        
        if (multipleFillinQuestion.getBlankAssesment() != null) {
            assessmentBlank = multipleFillinQuestion.getBlankAssesment();
        }
        
        panel.setStudentAnswer(multipleFillinQuestion.getStudentAnswer());
//        String[]answer={"123","yes"};
//        panel.setStudentAnswer(answer);
        panel.setAssessmentBlankPanel(multipleFillinQuestion.getBlankCount());
        panel.setAssesmentBlank(multipleFillinQuestion.getBlankAssesment());
        panel.setQuestionText(multipleFillinQuestion.getQuestionText());
        panel.setQuestion(question);
    }
    
    /*
     * Listener
     */
    
    public void fillInBlankListDidChange(javax.swing.event.ListSelectionEvent evt) {
        if (!evt.getValueIsAdjusting()) {  
            delegate.getDetailPanel().removeAll();
            delegate.getDetailPanel().add(markAnswerPanel);
            Question question = (Question)delegate.getFillInBlankList().getSelectedValue();
            invokeComponent(question);
            delegate.pack();  
       } 
    }
}
